/**
 * Created by Administrator on 2017/6/12.
 */
var Promise = require('bluebird');
var Movie = Promise.promisifyAll(require('../models/movie'));
var Comment = Promise.promisifyAll(require('../models/comment'));
var Category = Promise.promisifyAll(require('../models/category'));

//movie列表页
exports.list = function *(req, res) {
  var _movies = yield Movie.findAll();
  res.render('movie/movieList', {
    title: 'movie列表页',
    movies: _movies
  });
}

//movie录入页
exports.add = function *(req, res) {
  var id = req.query.id;
  var _categories = yield Category.findAll();
  if (id) {
    var _movie = yield Movie.findById(id);
    var cateInMovie = new Array();
    for (var i = 0; i < _movie.categories.length; i++) {
      cateInMovie.push(_movie.categories[i]._id + '');
    }
    res.render('movie/movieAdd', {
      title: 'movie录入页',
      movie: _movie,
      categories: _categories,
      cateInMovie: cateInMovie
    });
  } else {
    res.render('movie/movieAdd', {
      title: 'movie录入页',
      movie: {
        name: '机械战警',
        director: '何塞·帕迪里亚',
        year: 2014,
        poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5',
        play: 'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
        summary: '2028年，专事军火开发的机器人公司Omni Corp.生产了大量装备精良的机械战警'
      },
      categories: _categories
    });
  }
}

//movie详情页
exports.detail = function *(req, res) {
  var id = req.params.id;
  var _movie = yield Movie.findOne({_id: id});
  var _commments = yield Comment.findByMovieId(_movie._id);
  res.render('movie/movieDetail', {
    title: 'movie详情页',
    movie: _movie,
    comments: _commments
  });
}

//movie进行录入
exports.create = function *(req, res) {
  var movie = req.body;
  movie.time = Date.now();
  var newMovie = new Movie(movie);
  var _movie = yield newMovie.save();
  if (_movie) {
    if (movie.categories instanceof Array) {
      for (var i = 0; i < movie.categories.length; i++) {
        var _category = yield Category.findOne({_id: movie.categories[i]});
        _category.movies.push(_movie._id);
        yield Category.update({_id: movie.categories[i]}, _category);
      }
    } else {
      var _category = yield Category.findOne({_id: movie.categories});
      _category.movies.push(_movie._id);
      yield Category.update({_id: movie.categories}, _category);
    }
    res.redirect('/');
  }
}

//movie进行删除
exports.del = function *(req, res) {
  var id = req.body.id;
  var commandResult = yield Movie.remove({_id: id});
  if (commandResult.result.n == 1) {
    res.send('true');
  } else {
    res.send('false');
  }
}

//movie进行修改
exports.update = function *(req, res) {
  var movie = req.body;
  movie.time = Date.now();
  var doc = yield Movie.update({_id: movie.id}, movie);
  if (doc.nModified == 1) {
    res.redirect('/');
  }
}

//movie进行查询
exports.search = function *(req, res) {
  var movie = req.body;
  var _movies = yield Movie.findByName(movie.name);
  res.render('movie/movieSearch', {
    title: 'movie搜索页',
    search: movie.name,
    movies: _movies
  });
}